<?php
/**
 * Author：晴天 QQ：1186258278
 * Creation：2020/4/22 13:31
 * Filename：yile.php
 * 亿乐
 */

namespace lib\supply;


use Medoo\DB\SQL;

class yile
{
    /**
     * @param $param
     * @param $key
     * @return string
     * 亿乐/小储加密算法
     */
    public static function GetSign($param, $key)
    {
        $signPars = "";
        ksort($param);
        foreach ($param as $k => $v) {
            if ("sign" != $k && "" != $v) {
                $signPars .= $k . "=" . $v . "&";
            }
        }
        $signPars = trim($signPars, '&');
        $signPars .= $key;
        $sign = md5($signPars);
        return $sign;
    }

    /**
     * @param $OrderData 订单信息
     * @param $Goods 商品信息
     * @param $TypeSupply 对接货源信息
     * 提交下单信息！
     */
    public static function Submit($OrderData, $Goods, $TypeSupply)
    {
        global $date, $conf;
        $DB = SQL::DB();

        $DataPost = [];
        $i = 1;
        foreach (json_decode($OrderData['input'], TRUE) as $value) {
            $DataPost = array_merge($DataPost, [
                'value' . $i => $value,
            ]);
            $i++;
        }

        $DataPost = array_merge([
            'api_token' => $TypeSupply['username'],
            'timestamp' => time(),
            'gid' => $Goods['goods_id'],
            'num' => ($OrderData['num'] * $Goods['quantity']),
        ], $DataPost);

        $DataPost = array_merge([
            'sign' => self::GetSign($DataPost, $TypeSupply['password'])
        ], $DataPost);

        $TypeSupply['url'] = str_replace(["http:", "https:", "/"], "", $TypeSupply['url']);

        $DataCurl = Api::SuppluCurl($TypeSupply['url'] . '.api.94sq.cn/api/order', $DataPost);
        if ($DataCurl == '') return false;
        $DataCurl = json_decode($DataCurl, TRUE);

        /**
         * 卡密转换
         */
        if (isset($DataCurl['data'][0]['card'])) {
            $ii = 0;
            foreach ($DataCurl['data'] as $v) {
                if ($v == '') continue;
                @$DB->insert('token', [
                    'uid' => $OrderData['uid'],
                    'gid' => $Goods['gid'],
                    'code' => json_decode($OrderData['input'], TRUE)[0],
                    'token' => $v . (empty($DataCurl['data'][$ii]['pass']) ? '' : ' | ' . $DataCurl['data'][$ii]['pass']),
                    'ip' => $OrderData['ip'],
                    'order' => $OrderData['order'],
                    'endtime' => $date,
                    'addtime' => $date,
                ]);
                $ii++;
            }
        }

        if (!isset($DataCurl['status'])) {
            $Msg = '下单信息获取失败!,请检查对接配置！';
            $code = $conf['SubmitState'];
            $money = 0;
            $order = -1;
        } else {
            $Msg = $DataCurl['message'];
            $code = ($DataCurl['status'] == 0 ? 1 : $conf['SubmitState']);
            $money = ($DataCurl['status'] == 0 ? $DataCurl['rmb'] : 0);
            $order = ($DataCurl['status'] == 0 ? $DataCurl['id'] : -1);
        }

        return [
            'code' => $code,
            'msg' => $Msg,
            'money' => $money,
            'order' => $order,
        ];
    }

    /**
     * @param $id 订单号
     * @param $TypeSupply 对接货源信息
     */
    public static function Query($id, $TypeSupply)
    {
        $DataPost = [
            'api_token' => $TypeSupply['username'],
            'timestamp' => time(),
            'id' => $id
        ];

        $DataPost = array_merge([
            'sign' => self::GetSign($DataPost, $TypeSupply['password'])
        ], $DataPost);

        $TypeSupply['url'] = str_replace(["http:", "https:", "/"], "", $TypeSupply['url']);
        $DataCurl = Api::Curl($TypeSupply['url'] . '.api.94sq.cn/api/order/query', $DataPost);
        if ($DataCurl == '') return false;
        $DataCurl = json_decode($DataCurl, TRUE);
        if ($DataCurl['status'] <> 0) return false;
        $Data = $DataCurl['data'];

        switch ($Data['status']) {
            case 0:
                $order_state = '待处理';
                break;
            case 1:
                $order_state = '处理中';
                break;
            case 2:
                $order_state = '退单中';
                break;
            case 3:
                $order_state = '有异常';
                break;
            case 4:
                $order_state = '补单中';
                break;
            case 5:
                $order_state = '已更新';
                break;
            case 90:
                $order_state = '已完成';
                break;
            case 91:
                $order_state = '已退单';
                break;
            case 92:
                $order_state = '已退款';
                break;
            default:
                $order_state = '未知状态';
                break;
        }

        return [
            'ApiType' => 1,
            'ApiNum' => $Data['num'],
            'ApiTime' => $Data['created_at'],
            'ApiInitial' => $Data['start_num'],
            'ApiPresent' => $Data['now_num'],
            'ApiState' => $order_state,
            'ApiError' => ($Data['remark'] <> null ? $Data['remark'] : '无'),
        ];
    }
}